<template>
  <div>
    <el-dialog
      :title="type == 'add' ? '新增' : '修改'"
      :visible.sync="isShow"
      width="60%"
      :before-close="close"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- pid是上级的id 这里用下拉框 选项是上级的名字，选项的值是上级的id -->
        <!-- 下拉框的逻辑：发请求获取options的数据 循环数组生成option -->
        <el-form-item label="上级目录" prop="pid">
          <el-select v-model="ruleForm.pid" placeholder="请选择活动区域">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option
              v-for="(menu, index) in menuOptions"
              :key="index"
              :label="menu.title"
              :value="menu.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 1" label="图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 2" label="地址" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addMenu, editMenu } from "../../apis";
export default {
  props: ["isShow", "type"],
  data() {
    return {
      // 下拉的数据
      menuOptions: [],
      // 表单的数据
      ruleForm: {
        pid: 0,
        title: "",
        icon: "",
        url: "",
        type: 1,
        status: 1,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  created() {
    // 发请求 获取下拉菜单的数据
    this.getMenulist();
  },
  methods: {
    setVal(data) {
      // 给表单项赋值
      this.ruleForm = data;
    },
    getMenulist() {
      this.http("/menulist?istree=1").then((res) => {
        this.menuOptions = res.list;
      });
    },
    close() {
      this.$emit("close");
    },
    submit() {
      // 点击提交按钮
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 校验成功 可以提交数据了
          let res;
          // 先判断新增还是修改
          if(this.type == 'add') {
            res = await addMenu(this.ruleForm);
          }else {
            res = await editMenu(this.ruleForm);
          }
          if (res.code == 200) {
            this.$emit("update");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      this.close();
    },
  },
};
</script>

<style>
</style>